//
//  UIImage+DDAdd.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/7.
//

import UIKit

extension UIImage {
    
    func toMaxData(maxSizeInKB: Int) -> Data? {
        let maxSizeInBytes = maxSizeInKB * 1024
        var compression: CGFloat = 1.0
        var imageData = self.jpegData(compressionQuality: compression)

        while let data = imageData, data.count > maxSizeInBytes {
            compression -= 0.1
            if compression < 0 {
                return nil
            }
            imageData = self.jpegData(compressionQuality: compression)
        }

        return imageData
    }
}
